package com.hntxk.cqzd.consultation.service.impl;

import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.cqzd.consultation.entity.TblDibEmp;
import com.hntxk.cqzd.consultation.model.DibAndEmpModel;
import com.hntxk.cqzd.consultation.service.DibEmpService;
import com.hntxk.cqzd.flup.model.FlupAndDiacModel;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("dibEmpManageService")
@Transactional(rollbackFor = Exception.class)
public class DibEmpServiceImpl extends BaseServiceImpl<TblDibEmp> implements DibEmpService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.consultation.mapper.DibEmpMapper.";

    public DibEmpServiceImpl() {
        super(MAPPER_NAMESPACE);
    }


    @Override
    public ResultModel<DibAndEmpModel> insert(DibAndEmpModel dibAndEmpModel) {
        List<TblDibEmp> tblDibEmps = dibAndEmpModel.getDibEmp();

        for (TblDibEmp tblDibEmp : tblDibEmps) {
            getPrimaryKey(tblDibEmp);
            tblDibEmp.setDibId(dibAndEmpModel.getDibAndDiacModel().getDibId());
            super.insert(MAPPER_NAMESPACE + "insert", tblDibEmp);
            LogRecordUtil.addLog(new ActionLog(FlupAndDiacModel.class, tblDibEmp.getEmpId() + "", "12",
                    ActionType.ADD, "新增会诊人员：" + tblDibEmp.getEmpId() + "[ " + tblDibEmp.getEmpName()+ " ]"));

        }
        return ResultModel.empty();
    }

    @Override
    public ResultModel<DibAndEmpModel> update(DibAndEmpModel dibAndEmpModel) {

        //将数据库中与此主表相关的数据删除
        List<TblDibEmp> emps = getByFkId(dibAndEmpModel.getDibAndDiacModel().getDibId());
        if(emps.size()>0) {
            for(TblDibEmp dibEmp:emps){
                if(dibEmp.getEmpId()==null){
                    continue;
                }else{
                    delete(MAPPER_NAMESPACE+"delete",dibEmp);
                }
            }
        }
        return insert(dibAndEmpModel);
    }


    @Override
    public List<TblDibEmp> getByFkId(Long id) {
        TblDibEmp tblDibEmp=new TblDibEmp();
        tblDibEmp.setDibId(id);
        List<TblDibEmp> list = (List<TblDibEmp>) query(MAPPER_NAMESPACE + "getByFkId", tblDibEmp);
        return list;
    }

}
